import { GTRequestInterface } from './types'
import { Toast } from 'vant'
import { errorResult } from '../Axios'
import { GTAxiosOptions } from '../types'
import { LoginLocalStorageKey } from '../../hooks'

class GTRequest implements  GTRequestInterface {

  axiosOptions: GTAxiosOptions

  constructor(options: GTAxiosOptions){
    this.axiosOptions = options
  }

  isSuccess(responseData: any){
    const { code } = responseData
    if(code === 0){
      return true
    }
    return false
  }

  commonFail(responseData: any){
    const { desc, msg, code } = responseData

    // 特殊code错误处理
    if (code === 401) {
      return errorResult
    }
    console.log('----------commonFail', code)
    if(code === 20103) { // 会话过期
      this.axiosOptions.loginStatusCatch && this.axiosOptions.loginStatusCatch(code)
      return errorResult
    }

    //HEYid selector 专用
    if(code === 20108 ){
      return errorResult
    }

    if (desc || msg) {
      Toast.fail(desc || msg)
      return errorResult
    }

  }

  addTokenToRequest(config: any){
    const key = localStorage.getItem(LoginLocalStorageKey.key)
    const SessionId = localStorage.getItem(LoginLocalStorageKey.session)
    const base  = { key, SessionId }
    const data = JSON.stringify(config.data || {})
    config.data = { base, data }
  }
}

export default GTRequest
